// pages/myorderbox/batchprinter/batchprinter.js
import {
  getBatchAcceptOrders,
  batchAccept
} from '../../../utils/networkRequest.js'
import {
  errorMsg
} from '../../../utils/tool.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orders: [],
    allSelected: false,
    isPrintering: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this;
    getBatchAcceptOrders((r) => {
      if (r.code == 0) {
        self.setData({
          orders: r.data
        })
      } else {
        errorMsg(r.message);
      }
    })
  },

  //图片预览
  previewImage: function (e) {
    var current = e.currentTarget.dataset.src;
    wx.previewImage({
      current: current[0], // 当前显示图片的http链接		  	
      urls: current // 需要预览的图片http链接列表	
    })
  },
  //选择订单
  onCheckItem: function (e) {
    let orders = this.data.orders;
    let index = e.currentTarget.dataset.index
    let cindex = e.currentTarget.dataset.cindex
    let allSelected = true;
    let quanxuan = true;
    orders[index].orders[cindex].selected = !orders[index].orders[cindex].selected;
    orders[index].orders.map(function (o, i) {
      if (!o.selected) {
        allSelected = false;
      }
    })
    orders[index].selected = allSelected;


    orders.map(function (o, i) {
      if (!o.selected) {
        quanxuan = false;
      }
      o.orders && o.orders.map(function (oo, ii) {
        if (!oo.selected) {
          quanxuan = false;
        }
      })
    })




    this.setData({
      orders: orders,
      allSelected: quanxuan
    })

  },
  //选择该用户的所有订单
  onCheckUser: function (e) {
    let index = e.currentTarget.dataset.index;
    let orders = this.data.orders;
    let quanxuan = true;
    orders[index].orders.map(function (o, i) {
      o.selected = !orders[index].selected;
    })
    orders[index].selected = !orders[index].selected;


    orders.map(function (o, i) {
      if (!o.selected) {
        quanxuan = false;
      }
      o.orders && o.orders.map(function (oo, ii) {
        if (!oo.selected) {
          quanxuan = false;
        }
      })
    })


    this.setData({
      orders: orders,
      allSelected: quanxuan
    })

  },
  popClose: function () {
    wx.navigateBack();
  },
  //全选
  onAllCheck: function () {
    let allSelected = this.data.allSelected;
    let orders = this.data.orders;
    orders.map(function (o, i) {
      o.selected = !allSelected
      o.orders && o.orders.map(function (oo, ii) {
        oo.selected = !allSelected
      })
    })

    this.setData({
      orders: orders,
      allSelected: !allSelected
    })
  },
  onPrinter: function () {
    let ids = [];
    let orders = this.data.orders;


    orders.map(function (o, i) {
      o.orders && o.orders.map(function (oo, ii) {
        if (oo.selected)
          ids.push(oo.oid);
      })
    })

    if (!ids[0]) {
      errorMsg("请先选择订单")
    } else {
      batchAccept(ids, (r) => {
        if (r.code == 0) {
          this.setData({
            isPrintering: true
          })
          // errorMsg(r.message)
        } else {
          errorMsg(r.message)
        }
      })
    } 
  } 
})